﻿using System;
using System.Data;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapBASTGAD(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveBASTGAD(DataRow dwData, DataRow dwDispo)
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryBASTGAD(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapBASTGAD(ref dwLocal, dwData);
            InsertBASTGAD(dwLocal);
            UpdateBASTGAD(dwLocal);
        }
        public void InsertBASTGAD(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;

            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO BASTGAD(BAGMANDANT, BAGART, BAGVAR, BAGFARBE, BAGPOS, BAGGART, BAGGVAR, BAGGFARBE, BAGFDZ, BAGABF, BAGKGNET, BAGKGBRUT, BAGDISPOKZ, BAGBEM, BAGVARKZ, BAGAUFMSL, BAGFILLER)"
                + "VALUES (:BAGMANDANT, :BAGART, :BAGVAR, :BAGFARBE, :BAGPOS, 'J100                ', '3  ', '          ', 8308, 0, 98126, 98126, 'B', '                    ', ' ', '  ', '                  ')";
            or1.Parameters.Add("BAGMANDANT", OracleType.NVarChar).Value = dwData["BAGMANDANT"].ToString();
            or1.Parameters.Add("BAGART", OracleType.NVarChar).Value = dwData["BAGART"].ToString();
            or1.Parameters.Add("BAGVAR", OracleType.NVarChar).Value = dwData["BAGVAR"].ToString();
            or1.Parameters.Add("BAGFARBE", OracleType.NVarChar).Value = dwData["BAGFARBE"].ToString();
            or1.Parameters.Add("BAGPOS", OracleType.NVarChar).Value = dwData["BAGPOS"].ToString();
            setResult = Connect2Proxy(or1);
        }
        public void DeleteBASTGAD(string client, string art, string var, string color)
        {
            try
            {
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "DELETE BASTGAD WHERE  "
                + "  BAGMANDANT=:BAGMANDANT AND BAGART=:BAGART AND BAGVAR=:BAGVAR AND BAGFARBE=:BAGFARBE";
                or1.Parameters.Add("BAGMANDANT", OracleType.NVarChar).Value = client;
                or1.Parameters.Add("BAGART", OracleType.NVarChar).Value = art;
                or1.Parameters.Add("BAGVAR", OracleType.NVarChar).Value = var;
                or1.Parameters.Add("BAGFARBE", OracleType.NVarChar).Value = color;
                Connect2Proxy(or1);
            }
            catch (Exception ex)
            { 
            }
        }
        public DataSet QueryBASTGAD(DataRow dwData, int option = 1)
        {
            DataSet setReturn = new DataSet();
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            OracleCommand or1;

            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM BASTGAD WHERE  ROWNUM<2 ";
                    break;
                case 1:
                    or1.CommandText = "SELECT * FROM BASTGAD WHERE  "
                    + "  BAGMANDANT=:BAGMANDANT AND BAGART=:BAGART AND BAGVAR=:BAGVAR AND BAGFARBE=:BAGFARBE AND BAGPOS=:BAGPOS";
                    or1.Parameters.Add("BAGMANDANT", OracleType.NVarChar).Value = dwData["BAGMANDANT"].ToString();
                    or1.Parameters.Add("BAGART", OracleType.NVarChar).Value = dwData["BAGART"].ToString();
                    or1.Parameters.Add("BAGVAR", OracleType.NVarChar).Value = dwData["BAGVAR"].ToString();
                    or1.Parameters.Add("BAGFARBE", OracleType.NVarChar).Value = dwData["BAGFARBE"].ToString(); ;
                    or1.Parameters.Add("BAGPOS", OracleType.NVarChar).Value = dwData["BAGPOS"].ToString(); ;
                    break;
                case 4:
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "SELECT DISTINCT WF.* FROM WDECK WD RIGHT JOIN BASTGAD WF ON"
                        + "  BAGMANDANT=WDMANDANT  AND BAGART=WDART AND BAGVAR='1 '"
                        + " WHERE WDMANDANT=:WDMANDANT AND WDSA='K' AND WDTYP='B' AND WDWAKZ='F' AND WDAUFKRS=:WDAUFKRS  AND WDAUFLFD=:WDAUFLFD";
                    or1.Parameters.Add("WDMANDANT", OracleType.NVarChar).Value = dwData["WDMANDANT"].ToString();
                    or1.Parameters.Add("WDAUFKRS", OracleType.NVarChar).Value = dwData["WDAUFKRS"].ToString();
                    or1.Parameters.Add("WDAUFLFD", OracleType.NVarChar).Value = dwData["WDAUFLFD"].ToString();
                    break;
                default:
                    break;
            }

            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "BASTGAD";
            }
            setReturn.Merge(Result);
            return setReturn;
        }
    }
}
